package org.example.workbench.controller;

import org.example.commons.constant.Constant;
import org.example.commons.domain.ErrorInfo;
import org.example.commons.utils.DateFormatUtils;
import org.example.commons.utils.UUIDUtils;
import org.example.settings.domain.User;
import org.example.workbench.domain.CustomerRemark;
import org.example.workbench.service.CustomerRemarkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/customerRemark")
public class customerRemarkController {
    @Autowired
    private CustomerRemarkService customerRemarkService;

    @RequestMapping("/deleteCustomerRemarkById")
    public @ResponseBody Object deleteCustomerRemarkById(String id){
        ErrorInfo errorInfo = new ErrorInfo();
        try {
            int i = customerRemarkService.deleteCustomerRemarkById(id);
            if (i>0){
                errorInfo.setErrorCode(Constant.ERROR_INFO_CODE_SUCCESS);
            }else {
                errorInfo.setErrorCode(Constant.ERROR_INFO_CODE_FAIL);
                errorInfo.setErrorMessage("系统繁忙，请稍后.......");
            }
        }catch (Exception e){
            e.printStackTrace();
            errorInfo.setErrorCode(Constant.ERROR_INFO_CODE_FAIL);
            errorInfo.setErrorMessage("系统繁忙，请稍后.......");
        }
        return errorInfo;
    }
    @RequestMapping("/updateCustomerRemarkById")
    public @ResponseBody Object updateCustomerRemarkById(@RequestParam Map<String,Object> map,HttpSession session){
        User user = (User)session.getAttribute(Constant.SESSION_USER);
        map.put("editFlag","1");
        map.put("editTime",DateFormatUtils.formatDateTime(new Date()));
        map.put("editBy",user.getId());
        String id = (String) map.get("remarkId");
        ErrorInfo errorInfo = new ErrorInfo();
        try {
            int i = customerRemarkService.updateCustomerRemarkById(map);
            if (i>0){
                errorInfo.setErrorCode(Constant.ERROR_INFO_CODE_SUCCESS);
            }else {
                errorInfo.setErrorCode(Constant.ERROR_INFO_CODE_FAIL);
                errorInfo.setErrorMessage("系统繁忙，请稍后.......");
            }
        }catch (Exception e){
            e.printStackTrace();
            errorInfo.setErrorCode(Constant.ERROR_INFO_CODE_FAIL);
            errorInfo.setErrorMessage("系统繁忙，请稍后.......");
        }
        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("errorInfo",errorInfo);
        hashMap.put("customerRemark",map);
        return hashMap;
    }
    @RequestMapping("/insertCustomerRemarkByMap")
    public @ResponseBody Object insertCustomerRemarkByMap(@RequestParam Map<String,Object> map, HttpSession session){
        User user = (User)session.getAttribute(Constant.SESSION_USER);
        map.put("editFlag", Constant.EDIT_FLAG_NO);
        map.put("createBy",user.getId());
        map.put("createTime", DateFormatUtils.formatDateTime(new Date()));
        map.put("id", UUIDUtils.getUUID());
        ErrorInfo errorInfo = new ErrorInfo();
        try {
            int i = customerRemarkService.insertCustomerRemarkByMap(map);
            if (i>0){
                errorInfo.setErrorCode(Constant.ERROR_INFO_CODE_SUCCESS);
            }else {
                errorInfo.setErrorCode(Constant.ERROR_INFO_CODE_FAIL);
                errorInfo.setErrorMessage("系统繁忙，请稍后.......");
            }
        }catch (Exception e){
            e.printStackTrace();
            errorInfo.setErrorCode(Constant.ERROR_INFO_CODE_FAIL);
            errorInfo.setErrorMessage("系统繁忙，请稍后.......");
        }
        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("errorInfo",errorInfo);
        hashMap.put("customer",map);
        return hashMap;
    }
}
